//
//  ConSepcialVc.m
//  Shengzhuli
//
//  Created by FXGuo on 2018/2/6.
//  Copyright © 2018年 kamangkeji. All rights reserved.
//

#import "ConSepcialVc.h"
#import "SpecialListCell.h"
#import "ConBaseIntroVc.h"
#import "DetailInfoCell.h"
#import "TimeAndCountCell.h"
#import "BaseTipsCell.h"
#import "ConMapShowVc.h"
#import "ConBaseSelectTimeVc.h"
#import "NewHomeNetwork.h"
#import "MBProgressHUD+MJ.h"
#import "SZLAccountHelper.h"
#import "NSDate+Extension.h"
#import "NewPaySuccessVc.h"

@interface ConSepcialVc ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *specials;
@property (nonatomic,strong) UIView *detailView;
@property (nonatomic,strong) UITableView *detailTableView;
@property (nonatomic,strong) SpecialModel *selectModel;
@property (nonatomic,assign) UIButton *reserveBtn;

@property (nonatomic,strong) NSArray *selectTimeArr;//会所选定的时间
@property (nonatomic,assign) NSInteger perCount;//会所预定人数

@end

@implementation ConSepcialVc

- (void)viewDidLoad {
    [super viewDidLoad];
    self.perCount = 1;
    if (self.mapModel.clubType == 2) {  
        [self.view addSubview:self.tableView];
        [self.view addSubview:self.detailView];
        [self getspecialList];
    }else {
        [self.view addSubview:self.detailView];
        self.detailView.x = 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableView) {
        return 50;
    }else {
        switch (indexPath.row) {
            case 0:
                if (self.mapModel.clubType == 2) {
                    return [self.selectModel.name boundingRectWithSize:CGSizeMake(ScreenW-20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:Font(16)} context:nil].size.height + 129;
                }
                return 150;
                break;
            case 1:
                return 135;
            case 2:
                return [self.selectModel.content boundingRectWithSize:CGSizeMake(ScreenW-20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:Font(15)} context:nil].size.height + 66;
            default:
                break;
        }
    }
    return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.tableView) {
        return self.specials.count;
    }
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableView) {
        SpecialListCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SpecialListCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = self.specials[indexPath.row];
        return cell;
    }else {
        switch (indexPath.row) {
            case 0:
                {
                    DetailInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([DetailInfoCell class])];
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                    if (self.mapModel.clubType == 2) {
                        cell.model = self.selectModel;
                    }else {
                        cell.mapModel = self.mapModel;
                    }
                    
                    cell.mapBlock = ^{
                        ConMapShowVc * vc = [[ConMapShowVc alloc]init];
                        vc.mapModel = self.mapModel;
                        [self.navigationController pushViewController:vc animated:YES];
                    };
                    return cell;
                }
                break;
            case 1:
            {
                TimeAndCountCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([TimeAndCountCell class])];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.isActivity = self.activityId ?YES:NO;
                cell.maxReserve = self.selectModel.maxReserve;
                cell.timeSelectBlock = ^{
                    if (!self.activityId) {
                       [self selectTime];
                    }
                };
                cell.countBlock = ^(NSInteger count) {
                    self.selectModel.count = count;
                    self.perCount = count;
                };
                return cell;
            }
                break;
            case 2:
            {
                BaseTipsCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([BaseTipsCell class])];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.model = self.selectModel;
                return cell;
            }
                break;
            default:
                break;
        }
    }
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableView) {
        self.selectModel = self.specials[indexPath.row];
        //设置默认人数1
        self.selectModel.count = 1;
        [self.detailTableView reloadData];
        [UIView animateWithDuration:0.2 animations:^{
            self.detailView.x = 0;
        }];
        self.isPush = YES;
    }else {
        
    }
}
//选择时间操作
- (void)selectTime
{
    ConBaseSelectTimeVc *vc = [[ConBaseSelectTimeVc alloc]init];
    vc.model = self.selectModel;
    vc.popClickBlock = ^(NSArray *arr) {
        if (arr.count == 0) return ;
        self.selectModel.timeArr = arr;
        self.selectTimeArr = arr;
        TimeAndCountCell *cell = [self.detailTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
        cell.timeArr = arr;
    };
    [self.navigationController pushViewController:vc animated:YES];
}
//隐藏详情
- (void)setPushHidden
{
    [UIView animateWithDuration:0.2 animations:^{
        self.detailView.x = ScreenW;
    }];
    self.isPush = NO; 
}
//立即预定
- (void)reserveClick
{
    if ([SZLAccountHelper shouldLogin]) return;
    if (self.selectTimeArr.count == 0) {
        [self showOnlyText:@"请选择时间"];
        return;
    }
    if (!self.perCount) {
        [self showOnlyText:@"请选择人数"];
        return;
    }
    if (self.mapModel.clubType == 2) {
        self.buyBlock(self.selectModel);
    }else {
        [self huisuoReserve];
    }
    
}
//会所预定
- (void)huisuoReserve
{
    NSMutableDictionary *parames = [NSMutableDictionary dictionary];
    parames[@"token"] = [SZLAccountHelper shareInstance].token;
    parames[@"clubId"] = @(self.model.ID);
    parames[@"personNum"] = @(self.perCount);
    NSDate *date = self.selectTimeArr[0];
    parames[@"startDate"] = [date convertDateToSting];
    [self showWaiter];
    [NewHomeNetwork getCreateBaseSingleWithParame:parames response:^(BOOL success, NSString *msg,SingleCreateModel *model) {
        [self hideWaiter];
        if (success) {
            NewPaySuccessVc *vc = [[NewPaySuccessVc alloc]init];
            vc.isFromBase = YES;
            vc.isZero = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }else {
            [MBProgressHUD showError:msg];
        }
    }];
}
//基地商品列表
- (void)getspecialList
{
    NSMutableDictionary *parames = [NSMutableDictionary dictionary];
    parames[@"clubId"] = @(self.model.ID);
    if (self.activityId) {
        parames[@"activityId"] = @(self.activityId);
    }
    [self showWaiter];
    [NewHomeNetwork getSpecialListWithParame:parames response:^(BOOL success, NSString *msg, NSArray *lists) {
        [self hideWaiter];
        if (success) {
            if (lists.count == 0) {
                [self showNoDataLab:self.view range:CGRectMake(0, self.tableView.height *0.5-20, ScreenW, 20) title:@"暂无数据"];
                return ;
            }
            [self.specials addObjectsFromArray:lists];
            [self.tableView reloadData];
            if (self.activityId) {
                self.detailView.x = 0;
                self.selectModel = lists[0];
                self.selectModel.count = 1;
                //平台基地活动有时间
                [self.detailTableView reloadData];
                NSArray *arr = @[self.selectModel.startDate];
                self.selectModel.timeArr = arr;
                self.selectTimeArr = arr;
                TimeAndCountCell *cell = [self.detailTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
                cell.timeArr = arr;
            }
        }else {
            [MBProgressHUD showError:msg];
        }
    }];
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-self.roolH-32)];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SpecialListCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([SpecialListCell class])];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableFooterView = [[UIView alloc]init];
    }
    return _tableView;
}
- (UIView *)detailView
{
    if (!_detailView) {
        _detailView = [[UIView alloc]initWithFrame:CGRectMake(ScreenW, 0, ScreenW, ScreenH-self.roolH-32)];
        self.detailTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-self.roolH-32-45)];
        [self.detailTableView registerNib:[UINib nibWithNibName:NSStringFromClass([DetailInfoCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([DetailInfoCell class])];
        [self.detailTableView registerNib:[UINib nibWithNibName:NSStringFromClass([TimeAndCountCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([TimeAndCountCell class])];
        [self.detailTableView registerNib:[UINib nibWithNibName:NSStringFromClass([BaseTipsCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([BaseTipsCell class])];
        self.detailTableView.dataSource = self;
        self.detailTableView.delegate = self;
        self.detailTableView.separatorStyle = UITableViewCellSelectionStyleNone;;
        self.detailTableView.backgroundColor = [UIColor whiteColor];
        self.detailTableView.tableFooterView = [[UIView alloc]init];
        [_detailView addSubview:self.detailTableView];
        
        UIButton *reserveBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, _detailView.height-45, ScreenW, 45)];
        [reserveBtn setTitle:@"立即预订" forState:UIControlStateNormal];
        reserveBtn.titleLabel.font = Font(17);
        reserveBtn.backgroundColor = RGBColor(234, 111, 90);
        [reserveBtn addTarget:self action:@selector(reserveClick) forControlEvents:UIControlEventTouchUpInside];
        self.reserveBtn = reserveBtn;
        [_detailView addSubview:reserveBtn];
    }
    return _detailView;
}
- (NSMutableArray *)specials
{
    if (!_specials) {
        _specials = [NSMutableArray array];
    }
    return _specials;
}


@end
